from Products.Five import BrowserView
from Products.CMFPlone.utils import getToolByName
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from random import randint
from zope.component import getMultiAdapter

class homePageView(BrowserView):
    
    __call__ = ViewPageTemplateFile('templates/homepage.pt')
    
    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        #camitotalcarpeta = urltool.getPortalPath()
        cur_path = '/'.join(context.getPhysicalPath())
        camitotalcarpeta = cur_path + '/noticias'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #path['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
    
    def listVendedoresDelMes(self):
        """list images within folder 'vendedores-del-mes' into a uriach homeview_box
        """
        urltool = getToolByName(self.context, 'portal_url')
        portal_catalog = getToolByName(self.context, 'portal_catalog')
        
        path = urltool.getPortalPath() + '/vendedores-del-mes'
        #mypath = path+'/'+id
        mypath = path
        
        
        vendedores = portal_catalog.searchResults(portal_type = 'Image',
                                         path = mypath,
                                         sort_on='getObjPositionInParent')
        
        #self.context.plone_log('viewlets, l.35, path='+str(path))
        #self.context.plone_log('viewlets, l.36, folders='+str(folders))
        #import pdb;pdb.set_trace()
        if vendedores is None:
            return False
        else:
            return vendedores
    
    def listVendedoresByMajorBilling(self):
        urltool = getToolByName(self.context, 'portal_url')
        portal_catalog = getToolByName(self.context, 'portal_catalog')
        
        path = urltool.getPortalPath() + '/vendedores-major-billing'
        #mypath = path+'/'+id
        mypath = path
        
        
        vendedores = portal_catalog.searchResults(portal_type = 'Image',
                                         path = mypath,
                                         sort_on='getObjPositionInParent')
        
        #self.context.plone_log('viewlets, l.35, path='+str(path))
        #self.context.plone_log('viewlets, l.36, folders='+str(folders))
        #import pdb;pdb.set_trace()
        if vendedores is None:
            return False
        else:
            return vendedores
        
    def hasSpreadSheet(self):
        """Define which file will be used depending on the 'spreadsheet' plone page present within
           the folder 'resultados'
        """
        
    def getSpreadsheetResultadosAsImage(self, docu=None):
        urltool = getToolByName(self.context, 'portal_url')
        portal_catalog = getToolByName(self.context, 'portal_catalog')
        
        path = urltool.getPortalPath() + '/resultados'
        #mypath = path+'/'+id
        mypath = path
        
        
        results = portal_catalog.searchResults(portal_type = 'Image',
                                         path = mypath,
                                         sort_on='getObjPositionInParent')
        
        #self.context.plone_log('viewlets, l.35, path='+str(path))
        #self.context.plone_log('viewlets, l.36, folders='+str(folders))
        #import pdb;pdb.set_trace()
        if results is None:
            return False
        else:
            return results[:1]
     

    def getSpreadsheetResultadosAsHTML(self, docu=None):
        if docu is None:
             docu = 'spreadsheet'
#        
        #import pdb;pdb.set_trace()
#        #docu = 'prova_resultados'
        path = '/'.join(self.context.getPhysicalPath())
        docu_path = path + '/resultados/' + docu
        portal_url = getToolByName(self.context, 'portal_url')
        portal = portal_url.getPortalObject()
        try:
            obj = portal.unrestrictedTraverse(docu_path)
        except:
            return False
        
        content = obj.getText()
        #import pdb;pdb.set_trace()
        #return str(content[:650]) + '... <br/><a href="'+ docu_path +'" > Leer más...</a>'
        return content     